package cn.lyq.card.ui.achievement

import android.os.Bundle
import android.view.View
import cn.lyq.card.R
import cn.lyq.card.base.BaseFg
import cn.lyq.card.data.model.AchievementInfo
import cn.lyq.card.data.src.Status
import cn.lyq.card.global.BusTag
import cn.lyq.card.ui.agent.AgentBuyActivity
import cn.lyq.card.ui.agent.AgentListActivity
import cn.lyq.card.ui.income.IncomeDetailActivity
import cn.lyq.card.ui.qualified.QualifiedDeviceActivity
import cn.lyq.card.ui.store.StoreListActivity
import cn.lyq.card.utils.getVisibility
import cn.lyq.card.utils.setClick
import cn.lyq.card.utils.toMoney
import com.blankj.utilcode.util.BusUtils
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.fg_achievement_sub.*
import java.text.SimpleDateFormat
import java.util.*

class AchievementSubFg : BaseFg() {

    private val mModel by lazy {
        getVMActivity(AchievementVM::class.java)
    }
    private val daySdf = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
    private val monthSdf = SimpleDateFormat("yyyy-MM", Locale.CHINA)
    private val date = Calendar.getInstance()

    companion object {

        private const val KEY_TYPE = "TYPE"

        /**
         * 0日、1月、2全部
         */
        fun newInstance(type: Int): AchievementSubFg {
            val args = Bundle()
            args.putInt(KEY_TYPE, type)
            val fragment = AchievementSubFg()
            fragment.arguments = args
            return fragment
        }
    }

    private val type by lazy {
        arguments?.getInt(KEY_TYPE) ?: 0
    }

    override fun getLayoutId(): Int {
        return R.layout.fg_achievement_sub
    }

    override fun initView(view: View) {
        super.initView(view)
        tvAgentHint.setText(if (type == 2) R.string.str_agent_amount else R.string.str_agent_new)
        tvStoreHint.setText(if (type == 2) R.string.str_store_amount else R.string.str_store_new)

        tvSelectDate.visibility = getVisibility(type == 2)
        tvDate.visibility = getVisibility(type == 2)

        if (type == 0) {
            tvDate.text = daySdf.format(date.time)
        } else if (type == 1) {
            tvDate.text = monthSdf.format(date.time)
        }
    }

    override fun initListener(view: View) {
        super.initListener(view)
        tvSelectDate.setClick {
            DateSelectFg.newInstance(type, type == 0, date)
                .show(childFragmentManager, "DateSelectFg")
        }
        tvProfitHint.setClick {
            IncomeDetailActivity.start(requireActivity(), type, date)
        }
        tvAgentHint.setClick {
            AgentListActivity.start(requireActivity(), type, tvDate.text.toString())
        }
        tvStoreHint.setClick {
            StoreListActivity.start(requireContext(), type, tvDate.text.toString())
        }
        tvBuyHint.setClick {
            AgentBuyActivity.start(requireActivity(), type, tvDate.text.toString())
        }
        tvQualifiedHint.setClick {
            startActivity(QualifiedDeviceActivity::class.java)
        }
        refreshLayout.setOnRefreshListener {
            mModel.getInfo(type).postValue(null)
        }
    }

    override fun initObserver() {
        super.initObserver()
        mModel.getInfo(type).observe(this, androidx.lifecycle.Observer {
            if (it == null) {
                mModel.refresh(type, tvDate.text.toString())
            } else {
                when (it.status) {
                    Status.LOADING -> refreshLayout.isFocusable = true
                    Status.SUCCESS -> setData(it.requireData())
                    Status.ERROR -> {
                        ToastUtils.showShort(it.message.getMsg())
                        refreshLayout.isRefreshing = false
                    }
                }
            }
        })
        mModel.getInfo(type).postValue(null)
    }

    private fun setData(info: AchievementInfo) {
        tvTransactionMoney.text = info.volume.toMoney()
        tvTransactionNum.text = info.number.toString()
        tvProfit.text = "${info.profit.toMoney()}元"
        tvAgent.text = "${info.agentCount}人"
        tvStore.text = "${info.merchantCount}人"
        tvBuy.text = "${info.orderCount}台"
        refreshLayout.isRefreshing = false
    }

    @BusUtils.Bus(tag = BusTag.DATE_SELECT, threadMode = BusUtils.ThreadMode.MAIN)
    fun selectDate(params: Triple<Int, String, Long>) {
        if (type == params.first) {
            date.timeInMillis = params.third
            tvDate.text = params.second
            mModel.getInfo(type).postValue(null)
        }
    }
}